import regeneratorRuntime from '../../resource/libs/runtime'
import { customer_service_phone, reg } from '../../common/config'
import { wxApiPromise } from '../../common/utils'
import service from '../../common/service'

let app = getApp()
const page_config = {
  list: [
    {
      href: 'journey/index',
      icon: '/resource/images/journey-icon.png',
      text: '我的行程'
    },
    // {
    //   href: 'record/index',
    //   icon: '/resource/images/record-icon.png',
    //   text: '资金明细'
    // },
    // {
    //   href: 'balance/index',
    //   icon: '/resource/images/balance-icon.png',
    //   text: '余额充值'
    // },
    {
      href: 'feedback/index',
      icon: '/resource/images/feedback-icon.png',
      text: '意见反馈'
    },
    {
      href: '',
      icon: '/resource/images/contact-us-icon.png',
      text: '客服热线'
    },
  ]
}

Page({
  data: {
    ...page_config,
    init: false,
    phone: '',
    phone_focus: false,
    phone_input: '',
    code_loading: false,
    code_disabled: false,
    code_focus: false,
    code_text: '获取验证码',
    code: '',
    countdown: 60,
    submit_loading: false,
    balance: 0,
    show_ticket: false,
    ticket_money: 0,
    ticket_date: ''
  },

  onLoad () {
    let phone = app.global_data.phone
    this.setData({
      phone,
      init: true
    })
  },

  phoneInput ({ detail }) {
    let phone_input = detail.value
    this.setData({ phone_input })
  },

  async codeTap () {
    let phone = this.data.phone_input
    if (!reg.phone.test(phone)) {
      await wxApiPromise(wx.showModal, {
        title: '格式错误',
        content: '请检查手机号是否正确',
        showCancel: false
      })
      this.setData({ phone_focus: true })
      return
    }
    this.setData({
      code_loading: true,
      code_disabled: true
    })
    let res = await service.postPromise('sms/register', { phone })
    this.setData({ code_loading: false })
    if (!res) { 
      this.setData({ code_disabled: false })
      return
    }
    if (typeof res === 'number') {
      await wxApiPromise(wx.showModal, {
        title: '获取失败',
        showCancel: false
      })
      this.setData({ code_disabled: false })
      return
    }
    this.setData({ code_focus: true })
    let t = setInterval(() => {
      let countdown = this.data.countdown
      if (countdown === 0) {
        clearInterval(t)
        this.setData({
          code_disabled: false,
          code_text: '获取验证码',
          countdown: 60
        })
      } else {
        this.setData({
          code_text: countdown + ' s',
          countdown: countdown - 1
        })
      }
    }, 1000)
  },

  codeInput ({ detail }) {
    let code = detail.value
    this.setData({ code })
  },

  async submit () {
    let submit_loading = this.data.submit_loading
    if (submit_loading) { return }
    this.setData({ submit_loading: true })
    let phone = this.data.phone_input
    let code = this.data.code
    let res = await service.postPromise('user/bindPhone', { phone, code, scene: 1 })
    this.setData({ submit_loading: false })
    if (!res) { return }
    if (typeof res === 'number') {
      await wxApiPromise(wx.showModal, {
        title: '登录失败',
        content: '请检查验证码是否正确输入',
        showCancel: false
      })
      return
    }
    // this.setData({ phone })
    app.updateGlobalData({ phone })
    let { data } = res
    if (data.ticket === 1) {
      this.setData({
        show_ticket: true,
        ticket_money: data.money,
        ticket_date: data.time_end
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },

  async listTap ({ currentTarget }) {
    let list = this.data.list
    let index = currentTarget.dataset.index
    let item = list[index]
    if (item.text === '客服热线') {
      let res = await wxApiPromise(wx.showActionSheet, {
        itemList: customer_service_phone,
      })
      if (res) {
        wx.makePhoneCall({
          phoneNumber: customer_service_phone[res.tapIndex],
        })
      }
    } else {
      wx.navigateTo({
        url: item.href,
      })
    }
  },

  async logoutTap () {
    let res = await wxApiPromise(wx.showModal, {
      title: '确定要注销吗？',
    })
    if (res.confirm) {
      let out = await service.getPromise('user/login/out')
      if (!out) { return }
      if (typeof out === 'number') {
        await wxApiPromise(wx.showModal, {
          title: '注销失败',
          showCancel: false
        })
        return
      } else {
        app.updateGlobalData({ phone: '' })
        wx.navigateBack({
          delta: 1
        })
      }
    }
  }
})